<template>
    <div class="loginForm">
        <h3>系统登录</h3>
        <el-form :rules="rules" :model="loginForm" ref="loginForm">
            <el-form-item prop="username">
                <el-input size="small" placeholder="请填写用户名" v-model="loginForm.username"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" size="small" placeholder="请填写用户密码" v-model="loginForm.password"></el-input>
            </el-form-item>
            <el-form-item>
                <el-checkbox v-model="checked">记住密码</el-checkbox>
            </el-form-item>
            <el-button size="small" type="primary" class="loginBtn" @click="submitLogin">登录</el-button>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                rules : {
                    username : [{required: true,message : '请填写用户名',trigger:'blur'}],
                    password : [{required: true,message : '请填写用户密码',trigger:'blur'}],
                },
                loginForm :{
                    username:'root',
                    password:'root'
                },
                checked : true
            }
        },
        methods: {
            // 提交登录
            submitLogin() {
                this.$refs['loginForm'].validate((valid) => {
                    if (valid) {
                        this.$postRequest('/login',this.loginForm).then(resp => {
                            if (resp) {
                                window.sessionStorage.setItem('loginUser',JSON.stringify(resp.data));
                                this.$router.replace('/home');
                            }
                        })
                    } else {
                        this.$message.warning('请正确填写用户名和密码');
                        return false;
                    }
                });
            }
        }
    }
</script>

<style scoped>
    .loginForm {
        width: 360px;
        padding: 20px;
        border: 1px solid #d5d5d5;
        border-radius: 15px;
        margin: 130px auto;
        box-shadow: 0 0 15px #b9b9b9;
    }
    .loginForm h3{
        text-align: center;
    }
    .loginBtn {
        width: 100%;
    }
</style>